<script setup>
import { defineEmits, defineProps, onMounted, ref, watch } from 'vue'
import axios from '@/util/axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'initSmallTypeList'])
const handleClose = () => {
  formRef.value.resetFields()
  emit('update:modelValue', false)
}
const props = defineProps({
  id: {
    type: Number,
    default: -1,
    required: true
  },
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true
  }
})

const formRef = ref(null)
const form = ref({
  id: -1,
  name: '',
  remark: '',
  bigType: {
    id: ''
  }
})

const bigTypeSelectOption = ref([]);
const initBigTypeSelectList = async() => {
  const res = await axios.post('admin/bigType/listAll')
  bigTypeSelectOption.value = res.data.bigTypeList
}
onMounted(() => {
  initBigTypeSelectList()
})

const initFormData = async (id) => {
  const res = await axios.get(`admin/smallType/${id}`)
  form.value = res.data.smallType
}

watch(() => props.dialogVisible, () => {
  if (props.id !== -1) {
    initFormData(props.id)
  } else {
    form.value = {
      id: -1,
      name: '',
      remark: '',
      bigType: {
        id: ''
      }
    }
  }
})

const rules = ref({
  name: [
    { required: true, message: '请输入商品小类名称', trigger: 'blur' },
  ],
   remark: [
    { required: true, message: '请输入商品小类描述', trigger: 'blur' },
  ]
})

const handleConfirm = () => {
  formRef.value.validate(async(valid) => {
    if(valid) {
      if (form.value.bigType.id == '') {
        ElMessage.error("请选择所属大类")
        return
      }
      const result = await axios.post('admin/smallType/save', form.value)
      const data = result.data
      if (data.code == 0) {
        ElMessage.success("执行成功")
        formRef.value.resetFields()
        emit('initSmallTypeList')
        handleClose()
      } else {
        ElMessage.error(data.msg)
      }
    } else {
      console.log("fail")
    }
  })
}
</script>

<template>
  <el-dialog
    model-value="dialogVisible"
    :title="dialogTitle"
    width="30%"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="小类名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="所属大类">
        <el-select v-model="form.bigType.id" placeholder="请选择...">
          <el-option
            v-for="item in bigTypeSelectOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="小类描述" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4" />
      </el-form-item>
    </el-form>
     <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
